292B - Network Topology - CodeForces Solution


graphs implementation *1200

Please click on ads to support us..

Python Code:

while True:
    try:
        x = input()
        n, m = list(map(int, x.split()))
        freqs = {(i+1): 0 for i in range(n)}
        for i in range(m):
            x = input()
            a, b = list(map(int, x.split()))
            freqs[a] += 1
            freqs[b] += 1
                freq = {}
        for key, val in freqs.items():
            if val not in freq:
                freq[val] = 1
            else:
                freq[val] += 1
        
        if len(freq) == 2:
            if 1 in freq.keys():
                if freq[1] == 2:
                    print("bus topology")
                elif freq[1] == n - 1:
                    print("star topology")
                else:
                    print("unknown topology")
            else:
                print("unknown topology")
        elif len(freq) == 1:
            if 2 in freq.keys():
                print("ring topology")
            elif 1 in freq.keys():
                print("bus topology")
            else:
                print("unknown topology")
        else:
            print("unknown topology")

    except EOFError:
        break
   	 				 	    	 	    				

C++ Code:

#include "bits/stdc++.h"
using namespace std;
#define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2> &a) {in >> a.fr >> a.sc; return in;}
template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2> a) {out << a.fr << " " << a.sc; return out;}
template<typename T, typename T1>T amax(T &a, T1 b) {if (b > a)a = b; return a;}
template<typename T, typename T1>T amin(T &a, T1 b) {if (b < a)a = b; return a;}

const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;

const int N = 1e6 + 7;

const int MOD = 1e9 + 7;
const long long MOD2 = static_cast<long long>(MOD) * MOD;


/* ----------------------------------------------------------------------------------------------------------*/






void solve() {


	int n, m, u, v;
	cin >> n >> m;

	int degree[n + 1];

	for (int i = 1; i <= n; i++) {
		degree[i] = 0;

	}

	for (int i = 1; i <= m; i++) {
		cin >> u >> v;
		degree[u]++;
		degree[v]++;
	}


	int cnt1 = 0, cnt2 = 0, cntN = 0;

	for (int i = 1; i <= n; i++) {

		if (degree[i] == 1) {
			cnt1++;
		}

		if (degree[i] == 2) {
			cnt2++;
		}

		if (degree[i] == (n - 1)) {
			cntN++;
		}
	}



	if (cnt2 == n) {
		cout << "ring topology";
		return;
	}


	if (cnt2 == (n - 2) && cnt1 == 2) {
		cout << "bus topology";
		return;
	}




	if (cntN == 1 && cnt1 == (n - 1)) {
		cout << "star topology";
		return;
	}

	cout << "unknown topology";



}


signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#ifdef SIEVE
	sieve();
#endif
#ifdef NCR
	init();
#endif




	int t = 1;
	// cin >> t;
	while (t--) solve();
	return 0;
}



Comments

Submit
0 Comments
More Questions

307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number